//
//  StarsView.m
//  iOS-drawStar
//
//  Created by URANUS on 16/3/3.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "StarsControlView.h"
#import "DrawStarView.h"

#ifndef SelectFillColor
#define SelectFillColor [UIColor colorWithRed:1.000 green:0.800 blue:0.400 alpha:1.000]
#endif

#ifndef UnSelectFillColor
#define UnSelectFillColor [UIColor whiteColor]
#endif

#ifndef SelectStrokeColor
#define SelectStrokeColor [UIColor colorWithRed:1.000 green:0.800 blue:0.400 alpha:1.000]
#endif

#ifndef UnSelectStrokeColor
#define UnSelectStrokeColor [UIColor colorWithWhite:0.502 alpha:1.000]
#endif

@implementation StarsControlView
{
    NSArray *_starViewArray;
    CGSize _starSize;
}
#pragma  mark Override
-(void)drawRect:(CGRect)rect{
    
    if (self.starLineCount==0&&self.starRowCount!=0) {
        NSInteger starsLineCount = [self calculateStarLocationWithStarCount:self.starCount andLineOrRowCount:self.starRowCount];
        _starViewArray = [self drawStarsInRect:rect withLineCount:starsLineCount andRowCount:self.starRowCount];
    }else if(self.starRowCount==0&&self.starLineCount!=0){
        NSInteger starsRowCount = [self calculateStarLocationWithStarCount:self.starCount andLineOrRowCount:self.starLineCount];
        _starViewArray = [self drawStarsInRect:rect withLineCount:self.starLineCount andRowCount:starsRowCount];
    }else if(self.starLineCount!=0&&self.starRowCount!=0){
        _starViewArray = [self drawStarsInRect:rect withLineCount:self.starLineCount andRowCount:self.starRowCount];
        
    }
    [self resetStarsLevelShowWithLevel:_starsLevel];
}

/**
 *  @author X
 *
 *  通过给定的星星数量和行数（或者列数），计算范围内星星的列数（或者行数）
 *
 *  @param starCount     星星的数量
 *  @param count 星星的行数或者星星的列数
 *
 *  @return 星星的列数或者星星的行数
 */
#pragma mark Private
-(NSInteger)calculateStarLocationWithStarCount:(NSInteger)starCount andLineOrRowCount:(NSInteger)count{
    CGFloat rowOrLineCount = starCount/count;
    if ((starCount%count) != 0) {
        return rowOrLineCount+1;
    }else{
        return rowOrLineCount;
    }
    
}

/**
 *  @author X
 *
 *  在指定区域大小内分配固定行列的星星
 *  @param rect  绘制星星的区域大小
 *  @param lineCount 星星的行数
 *  @param rowCount  星星的列数
 *
 *  @return 返回包含星星的数组（便于之后更改内部各个星星的属性）
 */
#pragma mark Private
-(NSMutableArray *)drawStarsInRect:(CGRect)rect withLineCount:(NSInteger)lineCount andRowCount:(NSInteger)rowCount{
    
    CGFloat width = CGRectGetWidth(rect);
    CGFloat height = CGRectGetHeight(rect);
    CGFloat starWidth = width/rowCount>height/lineCount?height/lineCount:width/rowCount;
    NSMutableArray *starsArray = [NSMutableArray array];
    NSInteger starCount = self.starCount;
    for (int j=0; j<lineCount; j++) {
        for (int i=0; i<rowCount; i++) {
            if (starCount!=0) {
                DrawStarView *drawStartView = [self drawStarWithStarWidth:starWidth withIndexI:i andIndexJ:j];
                [starsArray addObject:drawStartView];
            }
            starCount--;
        }
    }
    return starsArray;
}
/**
 *  @author X
 *
 *  根据单个星星的大小，当前view的Rect中自动填充满星星
 *
 *  @param starSize 单个星星的大小
 */
-(void)autoDrawStarsWithStarSize:(CGSize)starSize{
    
    NSInteger tag = 10;
    NSArray *subViews = [self subviews];
    for (UIView *subView in subViews) {
        if (subView.tag==10) {
            [subView removeFromSuperview];
        }
    }
    _starSize = starSize;
    CGFloat width = CGRectGetWidth(self.frame);
    CGFloat height = CGRectGetHeight(self.frame);
    
    CGFloat starWidth = MIN(starSize.width, starSize.height);
    NSMutableArray *starsArray = [NSMutableArray array];
    NSInteger starCount = self.starCount;
    for (int j=0; starWidth*(j+1)<height; j++) {
        for (int i=0;starWidth*(i+1)<width;i++) {
            
            DrawStarView *drawStartView;
            if (self.starCount==0) {
                drawStartView = [self drawStarWithStarWidth:starWidth withIndexI:i andIndexJ:j];
                drawStartView.tag = tag;
                [starsArray addObject:drawStartView];
            }else{
                if (starCount>0) {
                    drawStartView = [self drawStarWithStarWidth:starWidth withIndexI:i andIndexJ:j];
                    drawStartView.tag = tag;
                    [starsArray addObject:drawStartView];
                }
                starCount--;
            }
            
        }
    }
    
    _starViewArray = [NSArray arrayWithArray:starsArray];
    [self resetStarsLevelShowWithLevel:_starsLevel];
    
}

/**
 *  @author X
 *
 *  在指定Index处，绘制指定大小的星星
 *
 *  @param starWidth 星星大小
 *  @param i         列位置
 *  @param j         行位置
 *
 *  @return 实际的星星View
 */
#pragma mark Public
-(DrawStarView *)drawStarWithStarWidth:(CGFloat)starWidth withIndexI:(NSInteger)i andIndexJ:(NSInteger)j{
    
    DrawStarView *drawStartView = [[DrawStarView alloc] initWithFrame:CGRectMake(starWidth*i, starWidth*j, starWidth, starWidth)];
    drawStartView.backgroundColor = [UIColor clearColor];
    drawStartView.startFillColor = self.unSelectedFillColor?self.unSelectedFillColor:UnSelectFillColor;
    drawStartView.startStrokeColor = self.unSelectedStrokeColor?self.unSelectedStrokeColor:UnSelectStrokeColor;
    [self addSubview:drawStartView];
    UITapGestureRecognizer *tapStarGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(starTap:)];
    [drawStartView addGestureRecognizer:tapStarGesture];
    
    return drawStartView;
    
}

#pragma mark Event Response
-(void)starTap:(UITapGestureRecognizer *)starTap{
    
    DrawStarView *drawStartViewTap = (DrawStarView*)starTap.view;
    NSInteger index = [_starViewArray indexOfObject:drawStartViewTap];
    _starsLevel = index+1;
    [self resetStarsLevelShowWithLevel:_starsLevel];
}

/**
 *  @author X
 *
 *  重新设置星星组的等级显示
 *
 *  @param level 星星组当前等级
 */
#pragma mark Private
-(void)resetStarsLevelShowWithLevel:(NSInteger)level{
    
    if (_starViewArray!=nil&&level<=_starViewArray.count) {
        
        for (int i = 0; i<_starViewArray.count; i++) {
            DrawStarView *drawStartView = (DrawStarView *)[_starViewArray objectAtIndex:i];
            drawStartView.startFillColor = self.unSelectedFillColor?self.unSelectedFillColor:UnSelectFillColor;
            drawStartView.startStrokeColor = self.unSelectedStrokeColor?self.unSelectedStrokeColor:UnSelectStrokeColor;
        }
        for (int i =0; i<level; i++) {
            DrawStarView *drawStartView = (DrawStarView *)[_starViewArray objectAtIndex:i];
            drawStartView.startFillColor = self.selectedFillColor?self.selectedFillColor:SelectFillColor;
            drawStartView.startStrokeColor = self.selectedStrokeColor?self.selectedStrokeColor:SelectStrokeColor;
        }
    }
    
    
}


#pragma mark Setter
-(void)setStarCount:(NSInteger)starCount{
    
    _starCount = starCount;
    if (starCount>0&&_starSize.width!=0&&_starSize.height!=0) {
        [self autoDrawStarsWithStarSize:_starSize];
    }
}

@end
